package com.wswy.wyjk.ui.main;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.my.httpapi.api.annotation.ViewL;
import com.my.httpapi.api.baseUtils.Toast;
import com.my.httpapi.api.baseUtils.Utils;
import com.my.httpapi.api.baseView.BaseActivity;
import com.tencent.mmkv.MMKV;
import com.wswy.wyjk.R;

import com.wswy.wyjk.api.Api;
import com.wswy.wyjk.api.MMKVConstant;
import com.wswy.wyjk.api.entity.RequestParameter;
import com.wswy.wyjk.ui.login.LoginActivity;
import com.wswy.wyjk.ui.main.model.ErrResponse;
import com.wswy.wyjk.ui.practiceNew.Dialog.CustomDialog;
import com.wswy.wyjk.ui.practiceNew.ExamActivity;
import com.wswy.wyjk.utils.Util;

import java.util.HashMap;
import java.util.Map;

@ViewL(R.layout.settting)
public class SettingActivity extends BaseActivity {
    private TextView tvCode, tvExit, tvCancellation;
    private ImageView ivSettingLogo;
    private LinearLayout llUserAgreement, llPrivacyPolicy, llNewVersion;
    private String token;

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();
        initData();
    }

    @Override
    protected String showTitle() {
        return "设置";
    }

    @Override
    protected boolean statusBarIsBlack() {
        return true;
    }

    @Override
    protected void destroy() {

    }

    private void initView() {
        isShowTitleLeft(View.VISIBLE);
        tvCode = findViewById(R.id.tv_code);
        tvExit = findViewById(R.id.tv_exit);
        tvCancellation = findViewById(R.id.tv_cancellation);
        ivSettingLogo = findViewById(R.id.iv_setting_logo);
        llUserAgreement = findViewById(R.id.ll_user_agreement);
        llPrivacyPolicy = findViewById(R.id.ll_privacy_policy);
        llNewVersion = findViewById(R.id.ll_new_version);
        tvCode.setText("V" + Utils.getAppVersionName(this));
        llPrivacyPolicy.setOnClickListener(view -> gotoWebView("隐私政策"));
        llUserAgreement.setOnClickListener(view -> gotoWebView("用户协议"));
        llNewVersion.setOnClickListener(v -> {
            Toast.show("已经是最新版本");
        });
        tvExit.setOnClickListener(view -> {
            new CustomDialog.Builder(SettingActivity.this).setTitle("确定要退出登录？")
                    .setPositiveButton((dialog, which) -> {
                        requestLoginOut();
                    }).create().show();
        });
        tvCancellation.setOnClickListener(view -> {
            new CustomDialog.Builder(SettingActivity.this).setTitle("确定要注销账户？")
                    .setPositiveButton((dialog, which) -> {
                        requestLoginCancel();
                    }).create().show();
        });
    }

    private void initData() {
        token = MMKV.defaultMMKV().decodeString(MMKVConstant.TOKEN);
        ivSettingLogo.setBackgroundResource(Util.getMetaDataInt(this, "setting_uplogo"));
        if (TextUtils.isEmpty(token)) {
            tvExit.setVisibility(View.GONE);
            tvCancellation.setVisibility(View.GONE);
        }
    }

    private void gotoWebView(String type) {
        Map map = new HashMap();
        map.put("type", type);
        intent(WebViewActivity.class, map);
    }

    private void requestLoginOut() {
        subscribe(Api.getApi().logout(RequestParameter.generate().getSingedParams()),
                request(data -> intentFinish(LoginActivity.class), err -> intentFinish(LoginActivity.class), true));
    }

    private void requestLoginCancel() {
        subscribe(Api.getApi().loginCancel(RequestParameter.generate().getSingedParams()),
                request(data -> {
                    Toast.show("账户已注销");
                    intentFinish(LoginActivity.class);
                }, true));
    }
}
